﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Framework;
using SchemaMonitor;

public partial class photo : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		CPhotoHash ph = null;

		var id = CWeb.RequestLong("id");
		if (id == long.MinValue)
		{
			var md5 = CWeb.RequestGuid("md5");
			ph = CPhotoHash.Cache.GetById(md5);
			Response.AddHeader("content-disposition", string.Concat("inline;filename=", CBinary.ToBase64(ph.PhotoMd5.ToByteArray()), ".jpg"));
		}
		else
		{
			var p = SchemaMonitor.CPhoto.GetPhoto(id);
			if (null == p)
				return;
			ph = p.PhotoHash;
			Response.AddHeader("content-disposition", string.Concat("inline;filename=", p.ID, ".jpg"));
		}
		if (null == ph)
			return;

		Response.Expires = 365 * 24 * 60;
        Response.Cache.SetExpires(DateTime.Now.AddYears(1));
        Response.Cache.SetCacheability(HttpCacheability.Public);

		var LOCAL = new CSqlClient("server=.;database=JewBookPhotos;Trusted_Connection=True;");

        Response.ContentType = "image/jpg";
		try
		{
			Response.BinaryWrite(ph.GetBlob(LOCAL));
		}
		catch
		{
			Response.BinaryWrite(ph.GetBlob());
		}
		Response.End();
	}
}